package com.itheima.service.user.impl;

import com.github.pagehelper.PageHelper;
import com.itheima.domain.Role;
import com.itheima.domain.UserInfo;
import com.itheima.factory.MapperFactory;
import com.itheima.mapper.user.UserMapper;
import com.itheima.service.user.UserService;
import com.itheima.utils.TransactionUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class UserServiceImpl implements UserService {
    /**
     * 分页查询用户列表
     * @param pageNum 分页参数:当前页
     * @param pageSize 分页参数:当前页条数
     * @return 返回:用户集合
     */
    @Override
    public List<UserInfo> list(String pageNum, String pageSize) {
        SqlSession sqlSession = null;
        List<UserInfo> list = null;
        try {
            //通过MapperFactory获取sqlSession对象
            sqlSession = MapperFactory.getSqlSession();
            //构建映射对象
            UserMapper mapper = sqlSession.getMapper(UserMapper.class);
            //设置分页初始参数
            int pageN = 1;
            int pageS = 5;
            //设置分页参数
            if (StringUtils.isNotBlank(pageNum)) {
                pageN = Integer.parseInt(pageNum);
            }
            if (StringUtils.isNotBlank(pageSize)) {
                pageS = Integer.parseInt(pageSize);
            }
            //分页工具实现分页功能
            PageHelper.startPage(pageN, pageS);
            //调用映射,读取数据库的用户信息
            list = mapper.list();
        } catch (Exception e) {
            //运行失败,回滚事务,抓取并打印错误信息到后台
            TransactionUtil.rollback(sqlSession);
            e.printStackTrace();
        } finally {
            //运行成功,提交事务,释放资源
            TransactionUtil.commit(sqlSession);
            TransactionUtil.close(sqlSession);
        }
        return list;
    }

    /**
     * 遍历用户列表
     * @return 返回:用户集合
     */
    @Override
    public List<UserInfo> findAll() {
        SqlSession sqlSession = null;
        List<UserInfo> list = null;
        try {
            //通过MapperFactory获取sqlSession对象
            sqlSession = MapperFactory.getSqlSession();
            //构建映射对象
            UserMapper mapper = sqlSession.getMapper(UserMapper.class);
            //调用映射,读取数据库的用户信息
            list = mapper.list();
        } catch (Exception e) {
            //运行失败,回滚事务,抓取并打印错误信息到后台
            TransactionUtil.rollback(sqlSession);
            e.printStackTrace();
            //提交事务
            TransactionUtil.commit(sqlSession);
        } finally {
            //释放资源
            TransactionUtil.close(sqlSession);
        }
        return list;
    }

    /**
     * 存储指定用户
     * @param user 参数:用户对象
     */
    @Override
    public void save(UserInfo user) {
        SqlSession sqlSession = null;
        try {
            //通过MapperFactory获取sqlSession对象
            sqlSession = MapperFactory.getSqlSession();
            //构建映射对象
            UserMapper mapper = sqlSession.getMapper(UserMapper.class);
            //调用映射,保存用户信息
            mapper.save(user);
            //提交事务
            TransactionUtil.commit(sqlSession);
        } catch (Exception e) {
            //运行失败,回滚事务,抓取并打印错误信息到后台
            TransactionUtil.rollback(sqlSession);
            e.printStackTrace();
        } finally {
            //释放资源
            TransactionUtil.close(sqlSession);
        }
    }

    /**
     * 根据id删除指定用户
     * @param ids 参数:id的字符串数组
     * @return 返回:删除结果(成功true/错误false)
     */
    @Override
    public boolean deleteByIds(String ids) {
        SqlSession sqlSession = null;
        boolean flag = false;
        try {
            //通过MapperFactory获取sqlSession对象
            sqlSession = MapperFactory.getSqlSession();
            //构建映射对象
            UserMapper mapper = sqlSession.getMapper(UserMapper.class);
            //将ids转化为字符串数组
            String[] idArr = ids.split(",");
            //调用映射,根据距id的字符串数组删除指定用户
            mapper.deleteByIds(idArr);
            //提交事务
            TransactionUtil.commit(sqlSession);
        } catch (Exception e) {
            //运行失败,回滚事务,抓取并打印错误信息到后台
            TransactionUtil.rollback(sqlSession);
            e.printStackTrace();
        } finally {
            //释放资源
            TransactionUtil.close(sqlSession);
        }
        flag =true;
        return flag;
    }

    /**
     * 根据指定字段对用户名模糊查询
     * @param name 参数:要进行查询的字段
     * @return 返回:包含指定字段的用户集合
     */
    @Override
    public List<UserInfo> findByName(String name) {
        SqlSession sqlSession = null;
        List<UserInfo> list = null;
        try {
            //通过MapperFactory获取sqlSession对象
            sqlSession = MapperFactory.getSqlSession();
            //构建映射对象
            UserMapper mapper = sqlSession.getMapper(UserMapper.class);
            //调用映射,根据距id查询指定用户
            list = mapper.findByName(name);
            //提交事务
            TransactionUtil.commit(sqlSession);
        } catch (Exception e) {
            //运行失败,回滚事务,抓取并打印错误信息到后台
            TransactionUtil.rollback(sqlSession);
            e.printStackTrace();
        } finally {
            //释放资源
            TransactionUtil.close(sqlSession);
        }
        return list;
    }

    /**修改用户状态
     *
     * @param id 参数:用户id
     */
    @Override
    public void changeStatusById(String id) {
        SqlSession sqlSession = null;
        try {
            //通过MapperFactory获取sqlSession对象
            sqlSession = MapperFactory.getSqlSession();
            //构建映射对象
            UserMapper mapper = sqlSession.getMapper(UserMapper.class);
            //根据id查询到指定用户
            UserInfo user = mapper.findById(id);
            //获取用户状态信息(0/1)
            int status = user.getStatus();
            // 是0变1,是1变0
            status = (status == 0 ? 1 : 0);
            //存回user对象
            user.setStatus(status);
            //调用映射,修改指定用户信息
            mapper.changeStatusById(user);
            //提交事务
            TransactionUtil.commit(sqlSession);
        } catch (Exception e) {
            //运行失败,回滚事务,抓取并打印错误信息到后台
            TransactionUtil.rollback(sqlSession);
            e.printStackTrace();
        } finally {
            //释放资源
            TransactionUtil.close(sqlSession);
        }
    }

    @Override
    public UserInfo findRoleById(String id) {
        SqlSession sqlSession = null;
        UserInfo user = null;
        try {
            //通过MapperFactory获取sqlSession对象
            sqlSession = MapperFactory.getSqlSession();
            //构建映射对象
            UserMapper mapper = sqlSession.getMapper(UserMapper.class);
            //调用映射,根据距id查询指定用户
            user = mapper.findRoleById(id);
            //提交事务
            TransactionUtil.commit(sqlSession);
        } catch (Exception e) {
            //运行失败,回滚事务,抓取并打印错误信息到后台
            TransactionUtil.rollback(sqlSession);
            e.printStackTrace();
        } finally {
            //释放资源
            TransactionUtil.close(sqlSession);
        }
        return user;
    }

    @Override
    public List<String> findPerById(String id) {
        SqlSession sqlSession = null;
        List<String> list=null;

        try {
            //通过MapperFactory获取sqlSession对象
            sqlSession = MapperFactory.getSqlSession();
            //构建映射对象
            UserMapper mapper = sqlSession.getMapper(UserMapper.class);
            //调用映射,根据距id查询指定用户
            list=mapper.findPerById(id);
            //提交事务
        } catch (Exception e) {
            //运行失败,回滚事务,抓取并打印错误信息到后台
            TransactionUtil.rollback(sqlSession);
            e.printStackTrace();
        } finally {
            //释放资源
            TransactionUtil.close(sqlSession);
        }
        return list;
    }

    @Override
    public List<UserInfo> findAllRole() {
        SqlSession sqlSession = null;
        List<UserInfo> list=null;

        try {
            //通过MapperFactory获取sqlSession对象
            sqlSession = MapperFactory.getSqlSession();
            //构建映射对象
            UserMapper mapper = sqlSession.getMapper(UserMapper.class);
            //调用映射,获取全部角色信息
            list=mapper.findAllRole();
            //提交事务
            sqlSession.commit();
        } catch (Exception e) {
            //运行失败,回滚事务,抓取并打印错误信息到后台
            TransactionUtil.rollback(sqlSession);
            e.printStackTrace();
        } finally {
            //释放资源
            TransactionUtil.close(sqlSession);
        }
        return list;
    }

    @Override
    public void changeRoleById(String id,String uid) {
        SqlSession sqlSession = null;
        try {
            //通过MapperFactory获取sqlSession对象
            sqlSession = MapperFactory.getSqlSession();
            //构建映射对象
            UserMapper mapper = sqlSession.getMapper(UserMapper.class);
            //根据id查询到指定用户
            UserInfo user = mapper.findById(id);
            //获取用户状态信息(0/1)
            int status = user.getStatus();
            // 是0变1,是1变0
            status = (status == 0 ? 1 : 0);
            //存回user对象
            user.setStatus(status);
            //调用映射,修改指定用户信息
            mapper.changeRoleById(id,uid);
            //提交事务
            TransactionUtil.commit(sqlSession);
        } catch (Exception e) {
            //运行失败,回滚事务,抓取并打印错误信息到后台
            TransactionUtil.rollback(sqlSession);
            e.printStackTrace();
        } finally {
            //释放资源
            TransactionUtil.close(sqlSession);
        }
    }

    /**
     * 根据id查询指定用户
     * @param id 参数:要查询的用户id
     * @return 返回:查询到的用户
     */
    @Override
    public UserInfo findById(String id) {
        SqlSession sqlSession = null;
        UserInfo user = null;
        try {
            //通过MapperFactory获取sqlSession对象
            sqlSession = MapperFactory.getSqlSession();
            //构建映射对象
            UserMapper mapper = sqlSession.getMapper(UserMapper.class);
            //调用映射,根据距id查询指定用户
            user = mapper.findById(id);
            //提交事务
            TransactionUtil.commit(sqlSession);
        } catch (Exception e) {
            //运行失败,回滚事务,抓取并打印错误信息到后台
            TransactionUtil.rollback(sqlSession);
            e.printStackTrace();
        } finally {
            //释放资源
            TransactionUtil.close(sqlSession);
        }
        return user;
    }

}
